import { Command } from '../constant';
import Taro from '@tarojs/taro';
import { Dispatch } from 'typings';
import { IAllReducerProps } from '../types';
import { getReducerData } from '@/redux/store';
import { extraPathsValue } from '@/redux/util';
import api from 'api';

export default (dispatch: Dispatch) => {
  let action = {
    commonChange(...param: any) {
      dispatch({
        type: Command.commonChange,
        payload: extraPathsValue(...arguments),
      });
    },
    async page() {
      let itemStr = Taro.getStorageSync('b2b-wechat@login');
      if (itemStr) {
        this.getAllHouse();
      }
    },
    /**
    * 查询房屋
    */
    async getAllHouse() {
      let { user } = Taro.getStorageSync('b2b-wechat@login');
      let selectDistance = Taro.getStorageSync('selectDistance');
      let params = {
        userId: user?.id || '',
        communityId: selectDistance.id,
      }
      let res = await api.houseController.findByUserId(params);
      if (res.code == '200') {
        let tempList = res.data;
        let currentHouse = [],
          otherHouse = [],
          houseId = '';
        if (res.data.length > 0) {
          tempList.map((item, index) => {
            if (item.defFlag == 1) {//当前房屋
              houseId = item.houseId;
              currentHouse.push(item);
            } else {
              otherHouse.push(item);
            }
          });
          action.commonChange([
            { paths: 'main.cardList', value: tempList },
            { paths: 'main.currentHouse', value: currentHouse },
            { paths: 'main.otherHouse', value: otherHouse },
          ]);
          this.findHouseUsers(houseId);
        }
      } else {
        Taro.showToast({
          title: res.msg,
          icon: 'none',
          duration: 2000,
        });
      }
    },
    /**
    * 查询房屋具体用户信息
    */
    async findHouseUsers(houseId) {
      let res = await api.houseController.findByHouseId({ houseId });
      if (res.code == '200') {
        let tempMembers = res.data;
        let owner = []; //业主
        let other = []; //其他身份
        if (res.data.length > 0) {
          tempMembers.map((item, index) => {
            if (item.houseHoldType == 1) {
              owner.push(item);
            } else {
              other.push(item);
            }
          });
          action.commonChange([
            { paths: 'main.owner', value: owner },
            { paths: 'main.other', value: other },
          ]);
        }
      }
    },
    /**
    * 解绑当前房屋(业主删除住户及自己)
    */
    async userDeletes(id) {
      let res = await api.houseController.unLockHouse({ id });
      if (res.code == '200') {
        action.commonChange('main.cardList', []);
        action.commonChange('main.currentHouse', []);
        action.commonChange('main.otherHouse', []);
        action.commonChange('main.owner', []);
        action.commonChange('main.other', []);
        this.getAllHouse();
        Taro.showToast({
          title: '解绑成功',
          icon: 'none',
          duration: 2000,
        });
      }
    },
    /**
    * 解绑其他房屋
    */
    async deleteHouse(id) {
      let res = await api.houseController.deleteHouse({ id });
      if (res.code == '200') {
        action.commonChange('main.cardList', []);
        action.commonChange('main.currentHouse', []);
        action.commonChange('main.otherHouse', []);
        action.commonChange('main.owner', []);
        action.commonChange('main.other', []);
        this.getAllHouse();
        Taro.showToast({
          title: '解绑成功',
          icon: 'none',
          duration: 2000,
        });
      }
    },
    /**
     * 审核住户申请
     */
    async houseProve(id, status) {
      let userInfo = (Taro.getStorageSync('b2b-wechat@login')).user;
      let { currentHouse } = getData().main;
      const params = {
        id,
        status,
        userId: userInfo.id,
      };
      let res = await api.houseController.houseProve(params);
      if (res.success) {
        Taro.showToast({
          title: res.msg,
          icon: 'none',
          duration: 2000,
        });
        setTimeout(() => {
          this.findHouseUsers(currentHouse[0].houseId)
        }, 1000);
      }
    }
  };
  return action;
};

function getData(): IAllReducerProps {
  return {
    main: getReducerData('packageAHouseMain'),
  };
}

//create by moon https://github.com/creasy2010/moon
